import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            redirect: '/manager/main',
            meta: { title: '首页' } // 添加 meta
        },
        {
            path: '/manager',
            component: () => import("../views/Manager.vue"),
            meta: { title: '管理平台' }, // 为父路由添加 meta 属性
            children: [
                // test
                { path: 'test', name: 'test', meta: { title: '测试' }, component: () => import('../views/ECharts.vue') },

                // summoners 路由
                {
                    path: 'summoners',
                    name: 'summoners',
                    meta: { title: '召唤师查询' }, // 更新标题
                    component: () => import('../views/summoners.vue'),
                    props: (route) => ({ // 添加 props 传递参数
                        puuid: route.query.puuid,
                        serverRegion: route.query.serverRegion,
                        // 添加其他必要参数...
                    }),
                    children: [
                        // {
                        //     path: '',
                        //     redirect: { name: 'default' },
                        //     meta: { title: '召唤师数据' } // 添加 meta
                        // },
                        {
                            path: 'default',
                            name: 'default',
                            meta: { title: '召唤师默认数据' }, // 添加 meta
                            component: () => import('../views/secondData/default.vue'),
                            props:true
                        },
                        {
                            path: 'champions',
                            name: 'champions',
                            meta: { title: '召唤师英雄数据' }, // 添加 meta
                            component: () => import('../views/secondData/champions.vue')
                        },
                        {
                            path: 'mastery',
                            name: 'mastery',
                            meta: { title: '英雄精通数据' }, // 添加 meta
                            component: () => import('../views/secondData/mastery.vue')
                        },
                        {
                            path: 'highlight',
                            name: 'highlight',
                            meta: { title: '召唤师精彩时刻' }, // 添加 meta
                            component: () => import('../views/secondData/highlight.vue')
                        },
                    ]
                },

                // home
                {
                    path: 'home',
                    name: 'home',
                    meta: { title: '平台主页' }, // 更新标题
                    component: () => import('../views/HomeView.vue')
                },

                // main
                {
                    path: 'main',
                    name: 'main',
                    meta: { title: '战绩查询' }, // 更新标题
                    component: () => import('../views/Main.vue')
                }
            ]
        },
        {
            path: '/404',
            name: 'Not Found',
            meta: { title: '页面未找到' }, // 更新标题
            component: () => import('../views/404.vue')
        },
        // 通配符路由 - 捕获所有未匹配的路由
        {
            path: '/:pathMatch(.*)*',
            redirect: '/404'
        }
    ],
})

// 修复后的导航守卫
router.beforeEach((to, from, next) => {
    // 安全地获取第一个包含 title 的 meta
    const routeWithTitle = to.matched.find(record => record.meta?.title);

    if (routeWithTitle) {
        document.title = routeWithTitle.meta.title;
    } else {
        // 默认标题
        document.title = '英雄联盟数据平台';
    }

    next();
})

export default router
